{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 无放回组合\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49a14f93-347c-42fc-ade2-6792329ccce3",
   "metadata": {},
   "source": [
    "该代码使用`itertools`库生成集合$\\{\"A\", \"B\", \"C\"\\}$中所有包含两个字母的组合。具体来说，代码调用`itertools.combinations`函数生成集合的组合，忽略元素顺序，且不允许重复。对于集合$\\{\"A\", \"B\", \"C\"\\}$，选择两个字母的组合情况为：\n",
    "\n",
    "$$\n",
    "(A, B), (A, C), (B, C)\n",
    "$$\n",
    "\n",
    "即二元组合数量为：\n",
    "\n",
    "$$\n",
    "\\binom{3}{2} = 3\n",
    "$$\n",
    "\n",
    "这展示了从三元素集合中选择两个元素的无序组合情况。代码依次输出每种组合，演示了如何从集合中生成固定大小的无重复组合，这种方法在组合数学、概率统计和数据选择中有广泛应用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8f483c8e-6a80-400e-8914-e51960933df0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('A', 'B')\n",
      "('A', 'C')\n",
      "('B', 'C')\n"
     ]
    }
   ],
   "source": [
    "## 查找包含2个字母的所有组合\n",
    "import itertools\n",
    "\n",
    "letters = \"ABC\"  # 定义字母集\n",
    "\n",
    "cmb = itertools.combinations(letters, 2)  # 生成包含2个字母的组合\n",
    "\n",
    "for val in cmb:\n",
    "    print(val)  # 输出每一个组合\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
